﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using QuantumWorkspace.SessionManager;

namespace OpenLS.Spreadsheet.UI.ViewModels
{
    public class RegisterViewModel : INotifyPropertyChanged, IDataErrorInfo
    {
        private string _error;
        private string _userName;
        private string _password;
        private string _rePassword;
        private string _email;

        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;

        private void notifyPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
        #endregion

        public bool Register()
        {

            if (StaticAuthenticationDAL.Instance.IsEmailAlreadyRegistered(this.Email))
            {
                this.Error = Internals.Resources.UIStringTable.EmailRegistered;
                return false;
            }
            else
            {
                bool isUserRegistered = StaticAuthenticationDAL.Instance.RegisterUser(this.UserName, this.Password, this.Email);
                return isUserRegistered;
            }
            return false;
        }


        public string this[string columnName]
        {
            get
            {
                if (columnName == "UserName")
                {
                    var regexItem = new Regex("[a-zA-Z0-9 ]*$");


                    if ((UserName.Length > 30 || UserName.Length < 5) || (!regexItem.IsMatch(UserName)))
                    {
                        return Internals.Resources.UIStringTable.InvalidUserName;
                    }
                }

                if (columnName == "Password")
                {
                    if (Password.Length < 6)
                        return Internals.Resources.UIStringTable.InvalidPasswordCreation;
                }
                if (columnName == "RePassword")
                {
                    if (RePassword.Length < 6)
                        return Internals.Resources.UIStringTable.InvalidPasswordCreation;
                    else
                    {
                        if (RePassword != Password)
                            return Internals.Resources.UIStringTable.PasswordsDoNotMatch;
                    }
                }

                if (columnName == "Email")
                {
                    Regex regex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
                    Match match = regex.Match(Email);
                    if (!match.Success)
                        return Internals.Resources.UIStringTable.PasswordsDoNotMatch;
                }

                return null;
            }
        }

        public string UserName
        {
            get
            {
                if (_userName == null)
                    _userName = "";
                return _userName;
            }
            set
            {
                _userName = value;
                notifyPropertyChanged("UserName");
            }
        }

        public string Password
        {
            get
            {
                if (_password == null)
                    _password = "";
                return _password;
            }
            set
            {
                _password = value;
                notifyPropertyChanged("Password");
            }
        }

        public string RePassword
        {
            get
            {
                if (_rePassword == null)
                    _rePassword = "";
                return _rePassword;
            }
            set
            {
                _rePassword = value;
                notifyPropertyChanged("RePassword");
            }
        }

        public string Email
        {
            get
            {
                if (_email == null)
                    _email = "";
                return _email;
            }
            set { _email = value; }
        }


        public string Error
        {
            get { return _error; }
            set
            {
                _error = value;
                notifyPropertyChanged("Error");
            }
        }

    }
}
